package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.enterwarehouse.domain.WEnterWarehouseInfo;
import com.ruoyi.enterwarehouse.service.IWEnterWarehouseInfoService;
import com.ruoyi.exitwarehouse.domain.WExitWarehouseInfo;
import com.ruoyi.exitwarehouse.service.IWExitWarehouseInfoService;
import com.ruoyi.finance.domain.TSalaryBillInfo;
import com.ruoyi.finance.service.ITSalaryBillInfoService;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.service.ITGoodsInfoService;
import com.ruoyi.statistic.domain.WDayExitWarehouseStat;
import com.ruoyi.statistic.service.IWDayExitWarehouseGoodsRelService;
import com.ruoyi.statistic.service.IWDayExitWarehouseStatService;
import com.ruoyi.statistic.domain.WMonthExitWarehouseStat;
import com.ruoyi.statistic.service.IWMonthExitWarehouseStatService;
import com.ruoyi.statistic.service.IWMonthExitWarehouseGoodsRelService;
import com.ruoyi.waybill.domain.TWayBillExt;
import com.ruoyi.waybill.service.ITWayBillExtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.sql.Date;
import java.time.LocalDate;

import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.Threads.sleep;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private IWEnterWarehouseInfoService wEnterWarehouseInfoService;

    @Autowired
    private ITGoodsInfoService tGoodsInfoService;

    @Autowired
    private IWExitWarehouseInfoService wExitWarehouseInfoService;

    @Autowired
    private IWDayExitWarehouseStatService wDayExitWarehouseStatService;

    @Autowired
    private IWDayExitWarehouseGoodsRelService wDayExitWarehouseGoodsRelService;

    @Autowired
    private IWMonthExitWarehouseStatService wMonthExitWarehouseStatService;

    @Autowired
    private IWMonthExitWarehouseGoodsRelService wMonthExitWarehouseGoodsRelService;

    private static Logger logger = LoggerFactory.getLogger(RyTask.class);

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    public void calculateEnterWeightPiece()
    {
        logger.debug("计算入库单，重量，件数，开始 ...");
        WEnterWarehouseInfo wEnterWarehouseInfo = new WEnterWarehouseInfo();
        List<WEnterWarehouseInfo> list = wEnterWarehouseInfoService.selectWEnterWarehouseInfoList(wEnterWarehouseInfo);
        for(WEnterWarehouseInfo enterWarehouseInfo : list)
        {
            wEnterWarehouseInfoService.updateWEnterTotalWeightPieceByNumber(enterWarehouseInfo.getEnterWarehouseNumber());
        }
        logger.debug("计算入库单，重量，件数，结束 ...");
    }

    public void calculateWarehouseLocate()
    {
        logger.debug("计算货物，仓位，开始 ...");
        TGoodsInfo tGoodsInfo = new TGoodsInfo();
        List<TGoodsInfo> list = tGoodsInfoService.selectTGoodsInfoList(tGoodsInfo);
        for(TGoodsInfo goods : list)
        {
            goods.setWarehouseTotalLocate(goods.getLogisticsParkName() + goods.getWarehouseLocateName() + goods.getRegionLocateName() + goods.getGoodsLocateName());
            tGoodsInfoService.updateTGoodsInfo(goods);
        }
        logger.debug("计算货物，仓位，结束 ...");
    }

    public void synchroniseTransportMark()
    {
        logger.debug("同步，运输标记，从出库单据到货物信息，开始 ...");
        WExitWarehouseInfo wExitWarehouseInfo = new WExitWarehouseInfo();
        List<WExitWarehouseInfo> list = wExitWarehouseInfoService.selectWExitWarehouseInfoList(wExitWarehouseInfo);
        for(WExitWarehouseInfo exitWarehouseInfo : list)
        {
            wExitWarehouseInfo.setExitWarehouseCustomer(exitWarehouseInfo.getExitWarehouseCustomer());
            wExitWarehouseInfo.setExitWarehouseNumber(exitWarehouseInfo.getExitWarehouseNumber());
            wExitWarehouseInfo.setExitTransportMark(exitWarehouseInfo.getExitTransportMark());
            wExitWarehouseInfoService.updateWExitWarehouseInfo(wExitWarehouseInfo);
        }
        logger.debug("同步，运输标记，从出库单据到货物信息，结束 ...");
    }

    /**
     * 统计日出库量，无参
     */

    public void statisticDayExitWarehouseTotal()
    {
        logger.debug("统计，日出库量，按合约号，开始 ...");
        /**
         * 计算当前日期，前一天日期
         */
        LocalDate  today = LocalDate.now();
        logger.debug("当前日期: " + today);

        // 获取前一天的日期
        LocalDate yesterday = today.minusDays(1);
        logger.debug("前一天日期: " + yesterday);

        TGoodsInfo tGoodsInfo = new TGoodsInfo();

        Double exitWarehouseWeightTotal = 0.0;

        WDayExitWarehouseStat wDayExitWarehouseStat = new WDayExitWarehouseStat();

        String dayExitStatisticNumber;

        Long dayExitStatisticId = 0L;

        List<Long> goodsIds = new ArrayList<>();
        /**
         * 按出库日期，筛选合约号列表
         */
        List<String> agreementNumbers = tGoodsInfoService.selectStatisticFieldByExitWarehouseDate(0 /*合约号*/, Date.valueOf(yesterday), Date.valueOf(yesterday));

        /**
         * 遍历合约号，货物
         */
        for(String agreementNumber : agreementNumbers)
        {

            /**
             * 按合约号。出库日期，查询货物
             */
            tGoodsInfo.setAgreementNumber(agreementNumber);
            tGoodsInfo.setExitWarehouseTime(Date.valueOf(yesterday));
//            logger.debug("OOO " + "statisticDayExitWarehouseTotal = " + tGoodsInfo);

            List<TGoodsInfo> goods = tGoodsInfoService.selectTGoodsInfoListUsedByDayStatistic(0 /*合约号*/, tGoodsInfo);
            /**
             * 合计出库货物重量，记录货物ID
             */
            for(TGoodsInfo good : goods)
            {
                exitWarehouseWeightTotal = exitWarehouseWeightTotal + good.getWeight();
                goodsIds.add(good.getGoodsId());
            }

            /**
             * 保护，goodsIds，为空，直接退出
             */
            if(goodsIds.isEmpty()) return;

            /**
             * 日出库量编号
             */
            dayExitStatisticNumber = OrderUtils.getOrderNumber("DS", "-");

            /**
             * 出库重量，四舍五入
             */
            exitWarehouseWeightTotal = Math.round(exitWarehouseWeightTotal * 1000.0) / 1000.0;

            /**
             * 日出库量，数据准备
             */
            wDayExitWarehouseStat.setDayExitStatisticNumber(dayExitStatisticNumber);
            wDayExitWarehouseStat.setExitWarehouseDate(Date.valueOf(yesterday));
            wDayExitWarehouseStat.setStatisticField("合约号");
            wDayExitWarehouseStat.setStatisticFieldName(agreementNumber);
            wDayExitWarehouseStat.setDayExitWarehouseWeight(exitWarehouseWeightTotal);
            wDayExitWarehouseStat.setCreateBy("system");
            wDayExitWarehouseStat.setCreateTime(DateUtils.getNowDate());

            /**
             * 日出库量，插入数据
             */
            wDayExitWarehouseStatService.insertWDayExitWarehouseStat(wDayExitWarehouseStat);
            sleep(300);
            /**
             * 获取插入记录，ID
             */
            dayExitStatisticId = wDayExitWarehouseStatService.selectDayExitStatisticIdByDayExitNumber(dayExitStatisticNumber);
//            logger.debug("OOO " + "dayExitStatisticId = " + dayExitStatisticId);

            /**
             * exitWarehouseWeightTotal清零
             */
            exitWarehouseWeightTotal = 0.0;


            /**
             * 入出库量关联表，数据插入
             */
//            logger.debug("OOO " + "goodsIds = " + goodsIds);
            wDayExitWarehouseGoodsRelService.insertWDayExitWarehouseGoodsRelExt(dayExitStatisticId, goodsIds);
            goodsIds.clear();

        }

        logger.debug("统计，日出库量，按合约号，结束 ...");
    }

    /**
     * 统计月出库量，无参
     */

    public void statisticMonthExitWarehouseTotal()
    {
        logger.debug("统计，月出库量，按合约号，开始 ...");
        /**
         * 计算当前日期，前一月日期
         */
        LocalDate currentDate = LocalDate.now();
        logger.debug("当前日期: " + currentDate);
        LocalDate lastMonthDate = currentDate.minusMonths(1);
        logger.debug("上月日期: " + lastMonthDate);
        LocalDate lastMonthFirstDay = lastMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        logger.debug("上个月第一天：" + lastMonthFirstDay);
        LocalDate lastMonthLastDay = lastMonthDate.with(TemporalAdjusters.lastDayOfMonth());
        logger.debug("上个月最后一天：" + lastMonthLastDay);

        TGoodsInfo tGoodsInfo = new TGoodsInfo();

        Double exitWarehouseWeightTotal = 0.0;

        WMonthExitWarehouseStat wMonthExitWarehouseStat = new WMonthExitWarehouseStat();

        String monthExitStatisticNumber;

        Long monthExitStatisticId = 0L;

        List<Long> goodsIds = new ArrayList<>();
        /**
         * 按出库日期，筛选合约号列表
         */
        List<String> agreementNumbers = tGoodsInfoService.selectStatisticFieldByExitWarehouseDate(0 /*合约号*/, Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));

        /**
         * 遍历合约号，货物
         */
        for(String agreementNumber : agreementNumbers)
        {

            /**
             * 按合约号。出库日期，查询货物
             */
            tGoodsInfo.setAgreementNumber(agreementNumber);
//            tGoodsInfo.setExitWarehouseTime(Date.valueOf(yesterday));
//            logger.debug("OOO " + "statisticMonthExitWarehouseTotal = " + tGoodsInfo);

            List<TGoodsInfo> goods = tGoodsInfoService.selectTGoodsInfoListUsedByMonthStatistic(0 /*合约号*/, tGoodsInfo, Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));
            /**
             * 合计出库货物重量，记录货物ID
             */
            for(TGoodsInfo good : goods)
            {
                exitWarehouseWeightTotal = exitWarehouseWeightTotal + good.getWeight();
                goodsIds.add(good.getGoodsId());
            }

            /**
             * 保护，goodsIds，为空，直接退出
             */
            if(goodsIds.isEmpty()) return;

            /**
             * 月出库量编号
             */
            monthExitStatisticNumber = OrderUtils.getOrderNumber("MS", "-");

            /**
             * 出库重量，四舍五入
             */
            exitWarehouseWeightTotal = Math.round(exitWarehouseWeightTotal * 1000.0) / 1000.0;

            /**
             * 月出库量，数据准备
             */
            wMonthExitWarehouseStat.setMonthExitStatisticNumber(monthExitStatisticNumber);
            wMonthExitWarehouseStat.setExitWarehouseDate(Date.valueOf(lastMonthDate));
            wMonthExitWarehouseStat.setStatisticField("合约号");
            wMonthExitWarehouseStat.setStatisticFieldName(agreementNumber);
            wMonthExitWarehouseStat.setMonthExitWarehouseWeight(exitWarehouseWeightTotal);
            wMonthExitWarehouseStat.setCreateBy("system");
            wMonthExitWarehouseStat.setCreateTime(DateUtils.getNowDate());

            /**
             * 月出库量，插入数据
             */
            wMonthExitWarehouseStatService.insertWMonthExitWarehouseStat(wMonthExitWarehouseStat);
            sleep(300);
            /**
             * 获取插入记录，ID
             */
            monthExitStatisticId = wMonthExitWarehouseStatService.selectMonthExitStatisticIdByMonthExitNumber(monthExitStatisticNumber);
//            logger.debug("OOO " + "monthExitStatisticId = " + monthExitStatisticId);

            /**
             * exitWarehouseWeightTotal清零
             */
            exitWarehouseWeightTotal = 0.0;


            /**
             * 入出库量关联表，数据插入
             */
//            logger.debug("OOO " + "goodsIds = " + goodsIds);
            wMonthExitWarehouseGoodsRelService.insertWMonthExitWarehouseGoodsRelExt(monthExitStatisticId, goodsIds);
            goodsIds.clear();

        }

        logger.debug("统计，月出库量，按合约号，结束 ...");
    }

}
